home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / windows5 / xwinc100.zip / CONTRIB-.00 / CONTRIB- / contrib / examples / Xaw / popup.c < prev    next >
C/C++ Source or Header  |  1991-01-22  |  10KB  |  315 lines

  1. /*
  2.  * An example of how to create a popup dialog box.
  3.  *
  4.  * August 17, 1989      - Donna Converse
  5.  * November 30, 1989    - Chris D. Peterson
  6.  */
  7.  
  8. /*
  9.  * $XConsortium: popup.c,v 1.8 91/01/22 19:24:22 gildea Exp $
  10.  *
  11.  * Copyright 1989 Massachusetts Institute of Technology
  12.  *
  13.  * Permission to use, copy, modify, distribute, and sell this software and its
  14.  * documentation for any purpose is hereby granted without fee, provided that
  15.  * the above copyright notice appear in all copies and that both that
  16.  * copyright notice and this permission notice appear in supporting
  17.  * documentation, and that the name of M.I.T. not be used in advertising or
  18.  * publicity pertaining to distribution of the software without specific,
  19.  * written prior permission.  M.I.T. makes no representations about the
  20.  * suitability of this software for any purpose.  It is provided "as is"
  21.  * without express or implied warranty.
  22.  *
  23.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  24.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  25.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  26.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  27.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  28.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  29.  */
  30.  
  31. #include <stdio.h>            /* For the Syntax message */
  32. #include <X11/Intrinsic.h>
  33. #include <X11/StringDefs.h>
  34. #include <X11/Shell.h>
  35.  
  36. #include <X11/Xaw/Box.h>
  37. #include <X11/Xaw/Cardinals.h>
  38. #include <X11/Xaw/Command.h>
  39. #include <X11/Xaw/Dialog.h>
  40.  
  41. static void PopupPrompt();    /* the callback of the main demo button */
  42. static void Quit();        /* the callback of the main quit button */
  43. static void ColorTheButton();    /* the callback of the popup ok button */
  44. static void DestroyPopupPrompt(); /* the callback of the popup cancel button */
  45. static void Ok();        /* an action proc calling ColorTheButton */
  46. static void Syntax();        /* report syntax errors and exit. */
  47. static int ConvertColor();    /* Converts a color name into a pixel value. */
  48.  
  49. static String fallback_resources[] = {
  50.     "*input: True",
  51.     "*go.label: Press to see Simple Popup Demonstration",
  52.     "*allowShellResize: True",
  53.     "*Dialog*value: ",
  54.     "*Dialog.label: What color should the main button be?",
  55.     "*Dialog*value.translations: #override \\n <Key>Return: Ok()",
  56.     "*Dialog*label.resizable: TRUE",
  57.     NULL,
  58. };
  59.  
  60. XtActionsRec actionTable[] = {
  61.     {"Ok",    Ok}
  62. };
  63.  
  64. void 
  65. main(argc, argv)
  66. int argc;
  67. char **argv;
  68. {
  69.     Widget    toplevel, button, box;
  70.     XtAppContext app_con;
  71.  
  72.     toplevel = XtAppInitialize(&app_con, "Popup", NULL, ZERO,
  73.                    &argc, argv, fallback_resources,
  74.                    NULL, ZERO);
  75.  
  76.     if (argc != 1) 
  77.     Syntax(app_con, argv[0]);
  78.  
  79.     XtAppAddActions(app_con, actionTable, XtNumber(actionTable));
  80.  
  81.     /* Two buttons, with callback routines, in a box. */
  82.  
  83.     box = XtCreateManagedWidget("box", boxWidgetClass, toplevel, NULL, ZERO);
  84.  
  85.     button = XtCreateManagedWidget("go", commandWidgetClass, box, NULL, ZERO);
  86.     XtAddCallback(button, XtNcallback, PopupPrompt, NULL);
  87.  
  88.     button = XtCreateManagedWidget("quit", commandWidgetClass, box, NULL,ZERO);
  89.     XtAddCallback(button, XtNcallback, Quit, NULL);
  90.  
  91.     XtRealizeWidget(toplevel);
  92.     XtAppMainLoop(app_con);
  93. }
  94.  
  95. /*    Function Name: PopupPrompt
  96.  *    Description: Creates and pops up the Dialog widget to ask for a new
  97.  *                   button color.
  98.  *    Arguments: button - the command button that activated this function.
  99.  *                 client_data, call_data - *** UNUSED ***
  100.  *    Returns: none.
  101.  */
  102.  
  103. /*ARGSUSED*/
  104. static void 
  105. PopupPrompt(button, client_data, call_data)
  106. Widget    button;        
  107. XtPointer client_data, call_data;
  108. {
  109.     Arg        args[5];
  110.     Widget    popup, dialog;
  111.     Position    x, y;
  112.     Dimension    width, height;
  113.     Cardinal    n;
  114.  
  115.     /*
  116.      * This will position the upper left hand corner of the popup at the
  117.      * center of the widget which invoked this callback, which will also
  118.      * become the parent of the popup.  I don't deal with the possibility
  119.      * that the popup will be all or partially off the edge of the screen.
  120.      */
  121.  
  122.     n = 0;
  123.     XtSetArg(args[0], XtNwidth, &width); n++;
  124.     XtSetArg(args[1], XtNheight, &height); n++;
  125.     XtGetValues(button, args, n);
  126.     XtTranslateCoords(button, (Position) (width / 2), (Position) (height / 2),
  127.               &x, &y);
  128.  
  129.     n = 0;
  130.     XtSetArg(args[n], XtNx, x);                n++;
  131.     XtSetArg(args[n], XtNy, y);                n++;
  132.  
  133.     popup = XtCreatePopupShell("prompt", transientShellWidgetClass, button,
  134.                    args, n);
  135.  
  136.     /* 
  137.      * The popup will contain a dialog box, prompting the user for input. 
  138.      */
  139.  
  140.     dialog = XtCreateManagedWidget("dialog", dialogWidgetClass, popup,NULL, 0);
  141.  
  142.     /*
  143.      * The prompting message's size is dynamic; allow it to request resize. 
  144.      */
  145.  
  146.     XawDialogAddButton(dialog, "ok", ColorTheButton, (XtPointer) dialog);
  147.     XawDialogAddButton(dialog, "cancel", DestroyPopupPrompt,(XtPointer)dialog);
  148.  
  149.     /*
  150.      * In this example, it is not necessary to call XtRealizeWidget.
  151.      * There are two situations where you may want to realize the
  152.      * popup before calling XtPopup:
  153.      *
  154.      * If you build your popups upon application startup, the time required
  155.      * to execute XtPopup is reduced if you realize your popups first.
  156.      *
  157.      * If you want to insure that the popup is not clipped by screen
  158.      * boundaries, realize the popup before calling XtGetValues on it.
  159.      *
  160.      * The dialog popup will allow user input to the rest of the application
  161.      * when the grab kind is GrabNone.   Since I allow input to the rest of
  162.      * the application and since I don't destroy the popup before creating a
  163.      * new one, I am allowing multiple identical popups, one for each time 
  164.      * the user clicks on the main application button.
  165.      *
  166.      * Try it with XtGrabExclusive instead of XtGrabNone.  Will the user
  167.      * be able to quit the application while the popup is up?  Does the
  168.      * command button which causes the popup to happen remain highlighted?
  169.      */
  170.  
  171.     XtPopup(popup, XtGrabNone);
  172. }
  173.  
  174. /*    Function Name: Quit
  175.  *    Description: exits the application.
  176.  *    Arguments: widget, client_data, call_data - *** UNUSED ***.
  177.  *    Returns: none
  178.  */
  179.  
  180. /*ARGSUSED*/
  181. static void 
  182. Quit(widget, client_data, call_data)
  183. Widget    widget;        
  184. XtPointer client_data, call_data;
  185. {
  186.     XtDestroyApplicationContext(XtWidgetToApplicationContext(widget));
  187.     exit(0);
  188. }
  189.  
  190. /*    Function Name: ColorTheButton
  191.  *    Description: This is a callback function that performs the
  192.  *                   actual task of coloring in the command button.
  193.  *    Arguments: w - *** UNUSED ***.
  194.  *                 client_data - a pointer to the dialog widget.
  195.  *                 call_data - *** UNUSED ***.
  196.  *    Returns: none
  197.  */
  198.  
  199. /*ARGSUSED*/
  200. static void 
  201. ColorTheButton(w, client_data, call_data)
  202. Widget    w;        
  203. XtPointer client_data, call_data;    
  204. {
  205.     Widget dialog = (Widget) client_data;
  206.     Widget button  = XtParent(XtParent(dialog)); 
  207.     int pixel;
  208.     String cname = XawDialogGetValueString(dialog);
  209.     Arg    args[3];
  210.  
  211.     pixel = ConvertColor(button, cname);
  212.  
  213.     if (pixel > 0) {
  214.     XtSetArg(args[0], XtNbackground, (Pixel) pixel);
  215.     XtSetValues(button, args, ONE);
  216.     DestroyPopupPrompt(NULL, (XtPointer) dialog, (XtPointer)NULL);
  217.     }
  218.     else {            /* pixel < 0  ==> error. */
  219.     char str[BUFSIZ];
  220.     (void) sprintf(str, "Can't get color \"%s\".  Try again.",  cname);
  221.        
  222.     XtSetArg(args[0], XtNlabel, str);
  223.     XtSetArg(args[1], XtNvalue, "");
  224.     XtSetValues(dialog, args, TWO);
  225.     }
  226. }
  227.  
  228. /*    Function Name: DestroyPopupPrompt
  229.  *    Description: Destroys the popup dialog widget.
  230.  *    Arguments: w - *** UNUSED ***.
  231.  *                 client_data - the dialog widget.  This widget is a direct
  232.  *                               child of the popup shell to destroy.
  233.  *                 call_data - *** UNUSED **.
  234.  *    Returns: none.
  235.  */
  236.  
  237. /*ARGSUSED*/
  238. static void 
  239. DestroyPopupPrompt(widget, client_data, call_data)
  240. Widget    widget;        
  241. XtPointer client_data, call_data;    
  242. {
  243.     Widget popup = XtParent( (Widget) client_data);
  244.     XtDestroyWidget(popup);
  245. }
  246.  
  247. /*    Function Name: Ok
  248.  *    Description: An action routine that is invoked from any child of 
  249.  *                   the dialog widget.  This routine will also color the
  250.  *                   command button that activated the popup.
  251.  *    Arguments: widget - the child of the dialog that caused this action.
  252.  *                 event, params, num_params - *** UNUSED ***.
  253.  *    Returns: none
  254.  */
  255.  
  256. /*ARGSUSED*/
  257. static void 
  258. Ok(widget, event, params, num_params)
  259. Widget widget;        
  260. XEvent *event;        
  261. String *params;    
  262. Cardinal *num_params;
  263. {
  264.     Widget dialog = XtParent(widget);
  265.  
  266.     ColorTheButton(widget, (XtPointer) dialog, (XtPointer) NULL);
  267. }
  268.  
  269. /*    Function Name: ConvertColor
  270.  *    Description: This converts a string into a color.
  271.  *    Arguments: color_name - name of the color.
  272.  *    Returns: a pixel value for that color.
  273.  */
  274.  
  275. static int
  276. ConvertColor(w, color_name)
  277. Widget w;
  278. char * color_name;
  279. {
  280.   XrmValue from, to;
  281.  
  282.   from.size = strlen(color_name) + 1;  
  283.   from.addr = color_name;
  284.  
  285. /*
  286.  * This conversion accepts a colorname from rgb.txt, or a #rrrgggbbb 
  287.  * rgb color definition, or the special toolkit strings "XtDefaultForeground" 
  288.  * and "XtDefaultBackground".
  289.  */
  290.  
  291.   XtConvert(w, XtRString, (XrmValuePtr) &from, XtRPixel, (XrmValuePtr) &to);
  292.   if (to.addr == NULL) {
  293.       return(-1);
  294.   }
  295.  
  296.   return( (int) *((Pixel *) to.addr) );
  297. }
  298.  
  299. /*    Function Name: Syntax
  300.  *    Description: Prints a the calling syntax for this function to stdout.
  301.  *    Arguments: app_con - the application context.
  302.  *                 call - the name of the application.
  303.  *    Returns: none - exits tho.
  304.  */
  305.  
  306. static void 
  307. Syntax(app_con, call)
  308. XtAppContext app_con;
  309. char *call;
  310. {
  311.     XtDestroyApplicationContext(app_con);
  312.     fprintf(stderr, "Usage: %s\n", call);
  313.     exit(1);
  314. }
  315.